<?php
error_reporting(7);

$charset = "utf-8";
// 检查安装文件是否存在
if (file_exists("./install.php")) {
        die("Alert: Please delete document 'install.php' before use the system");
}
require "./config.php";
require "./class/mysql.php";
require "./class/upload.php";
require "./adminfunctions.php";
require "./class/pagenav.php";
require "../include/enetoptions.php";
require "./class/imgcontrol.php";


$DB = new DB_MySQL;

$DB->servername=$servername;
$DB->dbname=$dbname;
$DB->dbusername=$dbusername;
$DB->dbpassword=$dbpassword;
$DB->mysqlver=$mysqlver;
$DB->dbcharset=$dbcharset;
$DB->connect();
$DB->selectdb();
$DB->query("SET NAMES 'utf8'"); 
include "./class/session.php";
require "./class/forms.php";
$cpforms = new FORMS;





if (intval(str_replace(".","",phpversion()))<410) {
    cpheader();
    pa_exit("PHP edition low,need >4.1.0 edition,your edition ".phpversion());
}


if(!get_magic_quotes_gpc()){
	$_POST=Add_S($_POST);
	$_GET=Add_S($_GET);
	$_COOKIE=Add_S($_COOKIE);
}

function Add_S($array){
	foreach($array as $key=>$value){
		if(!is_array($value)){
			$array[$key]=addslashes($value);
		}else{
			$array[$key]=Add_S($array[$key]);
		}
	}
	return $array;
}



if (!ini_get("register_globals")) {
    extract($_GET,EXTR_SKIP);
    extract($_POST,EXTR_SKIP);
}

require "configs/setting.php";
extract($configuration,EXTR_OVERWRITE);
define('HTMLDIR',$htmldir);
define('HTMLEXT',$htmlfileext);
$g_o_back2root='.';
unset($debug);
unset($showqueries);


unset($pauserinfo);
if ($_POST[action]=="login") {

    if (getuser_stat($_POST[username],md5($_POST[password]))) {
        $_SESSION[isadmin] = 1;
        $_SESSION[logined] = 1;
        $_SESSION[pauserinfo] = $pauserinfo;

        cpheader();
        redirect("./index.php","Login successfully,please wait......");
        cpfooter();
    } else {
        loginlog($_POST[username],$_POST[password],"Referer: ".getenv("HTTP_REFERER"));
        displaylogin();
    }
}

if (!$_SESSION[isadmin]&&(empty($_SESSION[ismanager])||!in_array($_GET['mod'],Array("mkarticle","mksort")))) {
	displaylogin();
}
$pauserinfo = $_SESSION[pauserinfo];
//$debug=1;


//terry
function fillzero($str){
   $str =  str_pad($str, 8, "0", STR_PAD_LEFT);
   return $str;
}

//all number
function allnumber($str ,$dot='0'){
 if($dot==0)
	{if(ereg("^(-{0,1}|\+{0,1})[0-9]+(\.{0,1}[0-9]{0,})$",$str)) return true;}
 elseif($dot==1)
	{if(ereg("^[0-9]+$",$str)) return true;}
 else{return false;}
}

//datetime

function datetime($str){
     $rs = date("Y-m-d",$str);
     return $rs;
}

//timedate

function timedate($str){
     $timestamp = strtotime($str);
     return $timestamp;
}

//select currency
function  CurrencySelect($id,$curname='')
{
    global $DB,$db_prefix;
	$res = $DB->query("SELECT * FROM ".$db_prefix."currency ORDER BY CurrencyID DESC");
	$str = '<select name="currency">';
	if($curname!=''){	$str = '<select name="'.$curname.'">';}
    while ($list = $DB->fetch_array($res)){
	   if($id == $list[Name]){ $other = " selected ";}
	   $str .= '<option value="'.$list[Name].'" '.$other.'>'.$list[Name].'</option>';
	   $other = '';
	}
    $str .= '</select>';
	return $str;
}



function SupplierSelect($sel){
    global $DB,$db_prefix;
   	$res = $DB->query("SELECT * FROM ".$db_prefix."supplier ORDER BY Id DESC");
	while($list = $DB->fetch_array($res)){
      if( $list[SupplierID] == $sel){ $other = " selected  ";}
	  $str .= '<option value="'.$list[SupplierID].'">'.$list[SupplierID].'&nbsp;&nbsp;&nbsp;|'.$list[SupplierName].'</option>';
	  $other = '';
	}
    return $str;
}

function checkoff($sid,$sess){
   if(@in_array($sid,$sess))return " checked ";
   else return "";
}

function RateExchange($money,$date,$type="RMB"){
    global $DB,$db_prefix;
    $timestamp = @strtotime($date);
   	$res  = $DB->query("SELECT * FROM ".$db_prefix."exchange WHERE StartDate <='".$timestamp."' AND EndDate >='".$timestamp."' AND Currency = '".$type."'");
	$list = $DB->fetch_array($res);
    if($list[ExchangeRate]!=''){
	   $re =  $money/$list[ExchangeRate];
	}else{
	   $re = '';
	} 
	return $re;
}


function isadmin(){
   if($_SESSION[pauserinfo][usergroupid]==1){
		return true;
   }else{
        return false;
   }
}

$storage = array('0'=>'office HH','1'=>'office SH','2'=>'office BD','3'=>'storage');
function Storage($id){
	global $storage;
    for($i = 0 ;$i<count($storage);$i++){
	   if($id == $storage[$i]){$selected = ' selected ';}
	   $str .= '<option value='.$i.' '.$selected.'>'.$storage[$i].'</option>';
	   $selected = '';
	}
    return $str;
}

function Isyard($str){
   if(preg_match ("/yard/i",$str)) $res = " selected ";
   else $res = '';
   return  $res;
}


function hotheader($str){
	echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">
						  <tr align="left">
					       <td width="20%" style="font-weight:bold;font-size:18px;color:blue;border-bottom:3px solid blue">'.$str.'</td>
				           <td width="50%">&nbsp;</td>
						   </tr>
				</table><br>';
}


function getSupplierNameBySid($sid){
   global $DB,$db_prefix;
   $str =  $DB->fetch_one_array("SELECT * FROM ".$db_prefix."supplier WHERE SupplierID='".$sid."'");
   return $str[SupplierName];
}

//jason12-8
function isvisitor(){
   if($_SESSION[pauserinfo][usergroupid]==3){
		return true;
   }else{
        return false;
   }
}
?>